package com.diandi.messages.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mxp on 2017/9/22.
 */
public class EmailUtils {
    private static Logger logger = LoggerFactory.getLogger(EmailUtils.class);

    /**
     * 通过第三方发送邮件
     *
     * @param paramMap
     */
    public static Map sendEmailBySohu(Map<String, String> paramMap) {
        Map returnMap = new HashMap();
        String result = "";
        try {
            Map<String, String> param = Maps.newHashMap();
            param.put("api_user", paramMap.get("apiUser"));
            param.put("api_key", paramMap.get("apiKey"));
            param.put("to", paramMap.get("toAddress"));
            param.put("from", paramMap.get("fromAddress"));
            param.put("fromname", "点滴身边");
            param.put("subject", paramMap.get("subject"));
            param.put("html", paramMap.get("content"));
            param.put("resp_email_id", "true");
            param.put("mail_from", paramMap.get("fromAddress"));
            result = HttpSender.sendPostByMap(paramMap.get("emailUrl"), param);
            JSONObject requestObject = JSONObject.parseObject(result);
            returnMap = requestObject;
            String message = requestObject.get("message").toString();
            if ("success".equals(message.trim())) {
                logger.info("邮箱:【{}】发送邮件成功", paramMap.get("toAddress"));
            } else {
                logger.info("邮箱:【{}】发送邮件失败返回的code：【{}】,错误信息【{}】", paramMap.get("toAddress"), message,
                        requestObject.get("errors"));
            }
        } catch (Exception e) {
            logger.error("邮件发送失败", e);
            returnMap.put("message", "error");
        }
        return returnMap;
    }
}